<template>
  <a-form :form="task2Form">
    <a-divider>增量设置</a-divider>
    <a-form-item
      label="增量类型"
      :labelCol="labelCol"
      :wrapperCol="wrapperCol">
      <a-select
        @change="(v)=>this.incrementType=v"
        v-decorator="['incrementType', {rules: [{ required: true, message: '请选择增量类型！' }]}]"
        placeholder="请选择增量类型"
        style="width: 100%"
      >
        <a-select-option v-for="(item,j) in incrementList" :key="j" :value="item">
          {{ item }}
        </a-select-option>
      </a-select>
    </a-form-item>
    <a-divider v-if="incrementType==='主键增量'">主键增量设置</a-divider>
    <a-row v-if="incrementType==='主键增量'" :gutter="24">
      <a-col :span="12">
        <a-form-item
          :labelCol="centerLabelCol"
          :wrapperCol="centerWrapperCol"
          label="增量参数"
        >
          <a-input
            v-decorator="['incrementParams', {rules: [{required: true, message: '请输入增量参数!'}]}]"
            placeholder="请输入增量参数" />
        </a-form-item>
      </a-col>
      <a-col :span="12">
        <a-form-item
          :labelCol="centerLabelCol"
          :wrapperCol="centerWrapperCol"
          label="增量开始ID"
        >
          <a-input
            :max-length="64"
            @blur="startIdChange"
            v-decorator="['incStartId', {rules: [{ required: true,message: '请输入增量开始ID!'}]}]"
            placeholder="请输入增量开始ID" />
        </a-form-item>
      </a-col>
    </a-row>
    <a-divider v-if="incrementType==='时间增量'">时间增量设置</a-divider>
    <a-row v-if="incrementType==='时间增量'" :gutter="24">
      <a-col :span="12">
        <a-form-item
          :labelCol="centerLabelCol"
          :wrapperCol="centerWrapperCol"
          label="增量参数"
        >
          <a-input
            v-decorator="['incrementParams', {rules: [{required: true, message: '请输入增量参数!'}]}]"
            placeholder="请输入增量参数" />
        </a-form-item>
      </a-col>
      <a-col :span="12">
        <a-form-item
          :labelCol="centerLabelCol"
          :wrapperCol="centerWrapperCol"
          label="增量开始时间"
        >
          <a-date-picker
            placeholder="请选择增量开始时间"
            mode="time"
            v-decorator="['incStartTime']"
            value-format="YYYY-MM-DD HH:mm:ss"
            format="YYYY-MM-DD HH:mm:ss"
            :showTime="true"
          />
        </a-form-item>
      </a-col>
    </a-row>
    <a-row v-if="incrementType==='时间增量'" :gutter="24">
      <a-col :span="12">
        <a-form-item
          :labelCol="centerLabelCol"
          :wrapperCol="centerWrapperCol"
          label="增量日期格式"
        >
          <a-input
            v-decorator="['incTimeFormatter', {rules: [{required: true, message: '请输入增量日期格式!'}]}]"
            placeholder="请输入增量日期格式" />
        </a-form-item>
      </a-col>
    </a-row>
    <a-divider v-if="incrementType!=='无'">SQL设置</a-divider>
    <a-form-item
      :labelCol="labelCol"
      :wrapperCol="wrapperCol"
      label="查询SQL"
      v-if="incrementType!=='无'"
    >
      <a-textarea
        :auto-size="{ minRows: 5}"
        v-decorator="['querySql', {rules: [{required: true, message: '请输入查询SQL!'}]}]"
        placeholder="请输入查询SQL" />
    </a-form-item>
  </a-form>
</template>

<script>
import { addCatchTask } from '@/api/modular/main/knowl/catchtask_config/catchTaskConfigManager'

export default {
  data() {
    return {
      task2Form: this.$form.createForm(this),
      labelCol: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 21 }
      },
      centerLabelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      centerWrapperCol: {
        xs: { span: 24 },
        sm: { span: 17 }
      },
      // 新增时使用
      responseValue: undefined,
      incrementType: undefined,
      incrementList: ['无', '主键增量', '时间增量'],
      detail: undefined
    }
  },
  methods: {
    setResponseValue(responseValue) {
      this.responseValue = responseValue
    },
    setDetail(e) {
      this.detail = e
    },
    startIdChange(e) {
      const { value } = e.target
      const formatValue = value.replace(/[^0-9]/ig, '')
      this.task2Form.setFieldsValue({
        'incStartId': formatValue
      })
    },
    onInit() {
      if (this.detail) {
        const entity = this.detail
        this.incrementType = entity.incrementType
        if (entity.incrementType === '无') {
          this.$nextTick(() => {
            this.task2Form.setFieldsValue(
              {
                incrementType: entity.incrementType
              }
            )
          })
        } else {
          this.$nextTick(() => {
            const time = this.parseTime(entity.incStartTime)
            this.task2Form.setFieldsValue(
              {
                incrementType: entity.incrementType,
                querySql: entity.querySql,
                incTimeFormatter: entity.incTimeFormatter,
                incrementParams: entity.incrementType === '时间增量' ? entity.incTimeColumn : entity.incIdColumn,
                incStartId: entity.incStartId,
                incStartTime: time
              }
            )
          })
        }
      } else {
        this.$nextTick(() => {
          this.task2Form.setFieldsValue(
            {
              incrementType: '主键增量'
            }
          )
        })
        this.incrementType = '主键增量'
      }
    },

    dispose() {
      this.task2Form.resetFields()
      this.detail = undefined
    },
    parseTime(time) {
      if (time) {
        return time
      } else {
        return this.$moment(new Date())
      }
    },
    insertValue(values, id) {
      const params = Object.assign(values)
      params['id'] = this.responseValue['id']
      params['step'] = 2
      const incrementType = this.incrementType
      if (incrementType === '时间增量') {
        params['incTimeColumn'] = values['incrementParams']
        const incrementStartTime = values['incStartTime']
        if (incrementStartTime) {
          params['incStartTime'] = incrementStartTime
        } else {
          params['incStartTime'] = this.$moment().format('YYYY-MM-DD HH:mm:ss')
        }
      } else if (incrementType === '主键增量') {
        params['incIdColumn'] = values['incrementParams']
      } else {
        params['incIdColumn'] = undefined
        params['incTimeColumn'] = undefined
        params['incStartTime'] = undefined
        params['querySql'] = undefined
        params['incStartId'] = undefined
      }
      this.$emit('loading')
      addCatchTask(params, id)
        .then((res) => {
          if (res.code === 200) {
            this.$emit('parentNext1', this.responseValue)
          }
        }).catch((e) => {
      })
        .finally(() => {
          this.$emit('dismiss')
        })
    },
    saveOrUpdate(id) {
      this.task2Form.validateFields((errors, values) => {
        if (!errors) {
          for (const key in values) {
            if (typeof (values[key]) === 'object') {
              values[key] = JSON.stringify(values[key])
            }
          }
          this.insertValue(values, id)
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
